
package com.epfresh.adapter;

import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.activity.CommonStoreActivity;
import com.epfresh.activity.ListStoreActivity;
import com.epfresh.api.universalimageloader.core.DisplayImageOptions;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.bean.MarketBean;
import com.epfresh.R;
import com.epfresh.views.FlexibleRoundedBitmapDisplayer;

import multitype.ItemViewBinder;
import multitype.OnRecyclerViewItemClickListener;

public class ViewBinderAreaMarket
    extends ItemViewBinder<MarketBean.MarketAreasBean, ViewBinderAreaMarket.ViewHolder> implements View.OnClickListener{

    OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;

    public OnRecyclerViewItemClickListener getOnRecyclerViewItemClickListener() {
        return onRecyclerViewItemClickListener;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_market_home_stores, parent, false);
        return new ViewHolder(view);
    }


    @Override
    protected void onBindViewHolder(@NonNull ViewHolder vhStores, @NonNull MarketBean.MarketAreasBean flag) {
        vhStores.tv_more.setTag(R.id.item_key_status, flag);
        vhStores.tv_more.setOnClickListener(this);

        vhStores.tv_depart_name.setText(flag.getName());
        if(flag.isMore()!=true){
            vhStores.tv_more.setVisibility(View.GONE);
        }else{
            vhStores.tv_more.setVisibility(View.VISIBLE);
        }
        for(int i=0;i<4;i++){
            vhStores.stores[i].setVisibility(View.INVISIBLE);
        }
        if(flag.getStores()==null)
            return;//判空校验
        if(flag.getStores().size()<=vhStores.storesNumber/2){
            vhStores.ll_store2.setVisibility(View.GONE);
        }else{
            vhStores.ll_store2.setVisibility(View.VISIBLE);
        }
        for (int i = 0; i < flag.getStores().size()&&i<vhStores.storesNumber; i++) {
            MarketBean.MarketAreasBean.StoresBean storesBean= flag.getStores().get(i);
            if(storesBean==null)
                continue;//判空校验
            vhStores.stores[i].setTag(R.id.item_key_status, storesBean);
            vhStores.iv_phone[i].setTag(R.id.item_key_status, storesBean);
            vhStores.stores[i].setOnClickListener(this);
            vhStores.iv_phone[i].setOnClickListener(this);

            vhStores.stores[i].setVisibility(View.VISIBLE);
            vhStores.tv_store_name[i].setText(storesBean.getName());
            if(storesBean.getStall()!=null) {
                vhStores.tv_store_number[i].setText(storesBean.getStall());
            }else{
                vhStores.tv_store_number[i].setText("");
            }
            vhStores.tv_goods_number[i].setText("共"+storesBean.getOnShelvesProductCount()+"件商品");
            vhStores.iv_flag[i].setVisibility(View.GONE);
            //ImageLoader.getInstance().displayImage(storesBean.getLogoUrl(), new ImageViewAware(vhStores.iv_store[i]));
            setRoundedImage(storesBean.getHeaderImage(), 4, FlexibleRoundedBitmapDisplayer.CORNER_TOP_LEFT|FlexibleRoundedBitmapDisplayer.CORNER_TOP_RIGHT,
                    R.mipmap.store_head_defaut,
                    vhStores.iv_store[i]
            );
            if(storesBean.getIsAcceptOrders()!=null&&storesBean.getIsAcceptOrders()==1){
                if(storesBean.getRecType()!=null&&storesBean.getRecType()==1){
                    vhStores.iv_flag[i].setVisibility(View.VISIBLE);
                    vhStores.iv_flag[i].setImageResource(R.mipmap.store_recent_triangle);
                }else if(storesBean.getRecType()!=null&&storesBean.getRecType()==2) {
                    vhStores.iv_flag[i].setVisibility(View.VISIBLE);
                    vhStores.iv_flag[i].setImageResource(R.mipmap.store_concern_triangle);
                }
            }else{
                vhStores.iv_flag[i].setVisibility(View.VISIBLE);
                vhStores.iv_flag[i].setImageResource(R.mipmap.store_sleep_triangle);
            }
        }

    }

    @Override
    public void onClick(View v) {
        Object o = v.getTag(R.id.item_key_status);
        if(v.getId()==R.id.tv_more &&  o instanceof MarketBean.MarketAreasBean) {
            MarketBean.MarketAreasBean marketAreasBean = (MarketBean.MarketAreasBean)o;
            Intent attention = new Intent(v.getContext(), ListStoreActivity.class);
            attention.putExtra("name", marketAreasBean.getName());
            attention.putExtra("marketId", marketAreasBean.getMarketId());
            attention.putExtra("areaId", marketAreasBean.getId());
            v.getContext().startActivity(attention);
        }else if(v.getId()==R.id.iv_phone && o instanceof MarketBean.MarketAreasBean.StoresBean){
            MarketBean.MarketAreasBean.StoresBean storesBeanX = (MarketBean.MarketAreasBean.StoresBean)o;
            Intent call = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + storesBeanX.getManagerPhone()));//调出来拨打页面；
            v.getContext().startActivity(call);
        }else if( o instanceof MarketBean.MarketAreasBean.StoresBean){
            MarketBean.MarketAreasBean.StoresBean storesBeanX = (MarketBean.MarketAreasBean.StoresBean)o;
            Intent storeIntent = new Intent(v.getContext(), CommonStoreActivity.class);
            storeIntent.putExtra("storeId", storesBeanX.getId());
            v.getContext().startActivity(storeIntent);
        }
    }


    static class ViewHolder extends RecyclerView.ViewHolder {

        int storesNumber = 4;
        TextView tv_depart_name;
        TextView tv_more;
        View ll_store1, ll_store2;
        View[] stores = new View[storesNumber];//v_1, v_2, v_3, v_4, v_5, v_6;
        TextView[] tv_store_name = new TextView[storesNumber];
        TextView[] tv_store_number = new TextView[storesNumber];
        TextView[] tv_goods_number = new TextView[storesNumber];
        ImageView[] iv_flag = new ImageView[storesNumber];
        ImageView[] iv_store = new ImageView[storesNumber];
        View[] iv_phone = new ImageView[storesNumber];


        ViewHolder(@NonNull View convertView) {
            super(convertView);
            tv_depart_name = (TextView) convertView.findViewById(R.id.tv_depart_name);
            tv_more = (TextView) convertView.findViewById(R.id.tv_more);
            ll_store1 = convertView.findViewById(R.id.ll_store1);
            ll_store2 = convertView.findViewById(R.id.ll_store2);
            stores[0] = convertView.findViewById(R.id.v_1);
            stores[1] = convertView.findViewById(R.id.v_2);
            stores[2] = convertView.findViewById(R.id.v_3);
            stores[3] = convertView.findViewById(R.id.v_4);

            for(int i=0;i<storesNumber;i++){
                tv_store_name[i] = (TextView) stores[i].findViewById(R.id.tv_store_name);
                tv_store_number[i] = (TextView) stores[i].findViewById(R.id.tv_store_number);
                tv_goods_number[i] = (TextView) stores[i].findViewById(R.id.tv_goods_number);
                iv_phone[i] = stores[i].findViewById(R.id.iv_phone);
                iv_flag[i] = (ImageView) stores[i].findViewById(R.id.iv_flag);
                iv_store[i] = (ImageView) stores[i].findViewById(R.id.iv_store);
            }
        }
    }

    public void setRoundedImage(String url, int cornerRadius, int corners, int image, ImageView imageView) {
        ImageLoader imageLoader = ImageLoader.getInstance();
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(image).showStubImage(image)
                .showImageForEmptyUri(image)//url为空时显示的图片
                .showImageOnFail(image)//加载失败显示的图片
                .cacheInMemory(true)// 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)// 设置下载的图片是否缓存在SD卡中
                .displayer(new com.epfresh.views.FlexibleRoundedBitmapDisplayer(cornerRadius,corners)) // 自定义增强型BitmapDisplayer
                .build();
        imageLoader.displayImage(url, imageView, options);
    }
}
